﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;

namespace CPPEI.PM.Service.Model.Repository
{
    public class WorkItemRepository
    {
        private DbContext _dbContext;
        public WorkItemRepository(DbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public WorkItem Get(Guid id)
        {
            return _dbContext.Set<WorkItem>().SingleOrDefault(p => p.Id == id);
        }

        public WorkItem GetByCode(Guid projectId, Guid workItemCategoryId, string code)
        {
            return _dbContext.Set<WorkItem>().SingleOrDefault(p => p.ProjectId == projectId && p.WorkItemCategoryId == workItemCategoryId && p.Code == code);
        }


        public List<WorkItem> GetWorkItemList(Guid projectId, Guid workSheetId)
        {
            return _dbContext.Set<WorkItem>().Join(_dbContext.Set<WorkItemRef>(),
                l => l.Id,
                r => r.WorkItemId,
                (l, r) => l).OrderBy(p=>p.CreatedTime)
                .ToList();
        }

        public List<WorkItem> GetList(Guid workItemCategoryId)
        {
            return _dbContext.Set<WorkItem>().Where(p => p.WorkItemCategoryId == workItemCategoryId).ToList();
        }

        public void CreateWorkItem(WorkItem workItem)
        {
            _dbContext.Set<WorkItem>().Add(workItem);
        }

        public void CreateWorkItemRef(WorkItemRef workItemRef)
        {
            _dbContext.Set<WorkItemRef>().Add(workItemRef);
        }
    }
}
